package com.hsnn.medstgmini.trading.pur.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.dict.model.form.DicAreaForm;
import com.hsnn.medstgmini.base.dict.service.DicAreaManager;
import com.hsnn.medstgmini.base.std.model.StdCompinfo;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.trading.pur.model.OrderPackStatus;
import com.hsnn.medstgmini.trading.pur.model.form.OrderPackage;
import com.hsnn.medstgmini.trading.pur.service.OrderConfirmManager;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 配送企业订单确认
 * @author 蔡春龙
 * @date 2015年7月13日
 */
@Controller
@RequestMapping("/orderConfirm")
public class OrderConfirmController {

	private static final Logger log = Logger
			.getLogger(OrderConfirmController.class);

	@Autowired
	private OrderConfirmManager orderConfirmManager;

	@Autowired
	private DicAreaManager dicAreaManager;

	@Value("#{sysProp['system.province']}")
	private String province;

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private HttpSession session;

	/**
	 * @category 获取当前登陆人信息
	 * @date 2015年6月14日
	 * @return
	 */
	public SysUser getUser() {
		return (SysUser) session.getAttribute(Constants.USERINFO);
	}

	/**
	 * @category 进入订单包列表页面
	 * @date 2015年7月13日
	 * @return
	 */
	@RequestMapping("/toOrderConfirm")
	public String toOrderConfirm() {
		DicAreaForm area = dicAreaManager.getDicAreaById(province);
		request.setAttribute("area", area);
		return "/trading/pur/orderConfirm";
	}

	/**
	 * @category 查询订单包列表
	 * @date 2015年7月13日
	 * @return
	 */
	@RequestMapping("/getOrderConfirm")
	@ResponseBody
	public Pagination getOrderConfirm(OrderPackage orderPackage) {
		log.info("[配送企业]查询订单包列表："+orderPackage);
		Pagination page = new Pagination(request);
		try {
			orderPackage.setOrderStatus(OrderPackStatus.pass.getKey());
			//	获取配送企业code
			StdCompinfo comp = getUser().getInfo();
			orderPackage.setDelCompCode(comp.getCompCode());
			orderConfirmManager.getOrderConfirm(orderPackage, page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 进入订单详细列表页面
	 * @date 2015年7月13日
	 * @return
	 */
	@RequestMapping("/toOrderConfirmDetail")
	public String toOrderConfirmDetail(Model model, String orderPackId) {
		log.info("[配送企业]进入订单详细列表页面ID："+orderPackId);
		Map<String, Object> map = orderConfirmManager.getOrderPackageById(orderPackId);
		model.addAttribute("orderPackId", orderPackId);
		model.addAttribute("orderPack", map);
		return "/trading/pur/orderConfirmDetail";
	}

	/**
	 * @category 查询订单详细列表
	 * @date 2015年7月13日
	 * @param orderPackage
	 * @return
	 */
	@RequestMapping("/getOrderConfirmDetail")
	@ResponseBody
	public Pagination getOrderConfirmDetail(OrderPackage orderPackage) {
		log.info("[配送企业]查询订单详细列表："+orderPackage);
		Pagination page = new Pagination(request);
		try {
			orderPackage.setOrderStatus(OrderPackStatus.submit.getKey());
			StdCompinfo comp = getUser().getInfo();
			orderPackage.setDelCompId(comp.getCompId());
			orderConfirmManager.getOrderConfirmDetail(orderPackage, page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
		}
		return page;
	}

	/**
	 * @category 确认或拒绝
	 * @date 2015年11月25日
	 * @param orderPackIds
	 * @param type1确认0拒绝
	 */
	@RequestMapping(value = {"/canDispatch", "/canNotDispatch"})
	@ResponseBody
	public Pagination dispatch(String orderPackIds, Integer type) {
		log.info("[配送企业]确认OR拒绝订单ID:"+orderPackIds+"订单类型："+type);
		Pagination page = new Pagination(request);
		try {
			if (StringUtils.isNotBlank(orderPackIds) && type != null) {
				SysUser user = getUser();
				orderConfirmManager.dispatch(orderPackIds, user, type);
				page.setSuccess(true);
			}
		} catch (Exception e) {
			log.error("",e);
		}
		return page;
	}

}
